import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Dialogs 1.3
import com.zyb.func 1.0

Item
{
    id: __root
    signal loadPath(string file)

    function preImage()
    {
        var len = IconModel.length()
        if(len > 0)
        {
            id_icon_view.currentIndex -= 1
            if(id_icon_view.currentIndex < 0)
                id_icon_view.currentIndex = len - 1
            loadPath( IconModel.filePath(id_icon_view.currentIndex) )
        }
    }

    function nextImage()
    {
        var len = IconModel.length()
        if(len > 0)
        {
            id_icon_view.currentIndex += 1
            if(id_icon_view.currentIndex >= len)
                id_icon_view.currentIndex = 0
            loadPath( IconModel.filePath(id_icon_view.currentIndex) )
        }
    }

    function doCopy() {
        ModelMgr.copy(id_icon_view.currentIndex)
    }

    GridView
    {
        id: id_icon_view
        anchors.fill: parent
        cellWidth: 200
        cellHeight: 140
        clip: true
        currentIndex: -1
        model: IconModel //ModelMgr.dataModel()
        boundsBehavior: Flickable.StopAtBounds
        boundsMovement: Flickable.StopAtBounds
        ScrollBar.vertical: ScrollBar {
            policy:  ScrollBar.AlwaysOn
            visible: id_icon_view.contentHeight > id_icon_view.height
        }
        delegate: Item {
            id: id_cell_item
            width:  170
            height: 130
            //默认黑色背景
            Item {
                width:  parent.width
                height: 96
                //加载图
                Image {
                    id: default_img
                    anchors.fill: parent
                    source: "qrc:///res/default.png"
                    fillMode: Image.PreserveAspectFit
                    cache: true
                }

                Image {
                    source: "image://thumb/" + path_role
                    anchors.fill: parent
                    fillMode: Image.PreserveAspectFit
                    asynchronous: true
                    visible: false
                    onStatusChanged: {
                        if(status === Image.Ready) {
                            visible = true
                            default_img.visible = false
                        }
                    }
                }
                //专门用来做选择时边框
                Rectangle {
                    anchors.fill: parent
                    color: "transparent"
                    border.width: 2
                    border.color: index === id_icon_view.currentIndex ? "#1EBD5D" : color
                }
            }

            //文本
            Text {
                text: name_role
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                color: id_cell_item.GridView.isCurrentItem ? "#1EBD5D" : "white"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 8
                width: parent.width - 8
                elide: Text.ElideRight
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    id_icon_view.currentIndex = index
                    loadPath(path_role)
                }
            }
        }
    }

    Button {
        text: "导入目录"
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        onClicked: id_folder_dialog.open()
    }

    FileDialog {
        id: id_folder_dialog
        folder: shortcuts.pictures
        selectFolder: true
        selectMultiple: false
        title: "Import a Picture Fold"
        onAccepted: {
            ModelMgr.scan(fileUrl)
        }
        Component.onCompleted: visible = false
    }
}
